package com.example.lijianwen.recyclerviewtest_401ljw.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;
import com.example.lijianwen.recyclerviewtest_401ljw.adapter.MyAdapter;
import com.example.lijianwen.recyclerviewtest_401ljw.R;
import com.example.lijianwen.recyclerviewtest_401ljw.adapter.TcareAdapter;
import com.example.lijianwen.recyclerviewtest_401ljw.bean.ArticleBean;
import com.example.lijianwen.recyclerviewtest_401ljw.bean.TcareBean;
import com.example.lijianwen.recyclerviewtest_401ljw.iface.ArticleListener;
import com.example.lijianwen.recyclerviewtest_401ljw.iface.TcareListener;
import com.example.lijianwen.recyclerviewtest_401ljw.model.ArticleModel;
import com.example.lijianwen.recyclerviewtest_401ljw.model.TcareModel;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    private MyAdapter mAdapter;
    private TcareAdapter tcareAdapter;
    private List<ArticleBean> list = null;
    private List<TcareBean> list1 = null;
//    private ArticleListener articleListener = new ArticleListener() {
//        @Override
//        public void onResponse(List<ArticleBean> beanlist) {
//            list = beanlist;
//            mAdapter.setList(list);
//            //数据更新的话
//            mAdapter.notifyDataSetChanged();
//        }
//
//        @Override
//        public void onFail(String msg) {
//            Toast.makeText(MainActivity.this,msg,Toast.LENGTH_SHORT);
//        }
//    };
    TcareListener tcareListener=new TcareListener() {
    @Override
    public void onResponse(List<TcareBean> list) {
        list1 = list;//得到网络数据
        tcareAdapter.setList(list);//传给adapter
        tcareAdapter.notifyDataSetChanged();//通知更新
    }

    @Override
    public void onFail(String msg) {
        Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
    }
};


    private void initRecyclerView() {
//        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
//        //创建默认的线性LayoutManager
//        layoutManager = new LinearLayoutManager(this);
//        //设置布局管理器
//        recyclerView.setLayoutManager(layoutManager);
//        //如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
//        recyclerView.setHasFixedSize(true);
//        //创建并设置Adapter
//        // list = getData();
//        mAdapter = new MyAdapter(this);
//        mAdapter.setList(list);
//        recyclerView.setAdapter(mAdapter);
        //获取RecyclerView，设置属性，获取数据源，绑定
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        //创建默认的线性布局
        layoutManager = new LinearLayoutManager(this);
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //固定每个item高度，提高性能
        recyclerView.setHasFixedSize(true);
        //获取数据并创建Adaper
        //  list=getData();
        tcareAdapter = new TcareAdapter(this);
        tcareAdapter.setList(list1);
//           myAdapter = new MyAdapter(this);
//            myAdapter.setList(list);
        //绑定Recycle人View和adapter
        recyclerView.setAdapter(tcareAdapter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initRecyclerView();
        //实例化ArticleModel,调用方法获取网络数据
        TcareModel tcareModel=new TcareModel();
        tcareModel.getResultList("tcase",1,"rv1l2tidbomann2qn86nln6ur2",tcareListener);
    }
/*
    private List<ArticleBean> getData() {
        List<ArticleBean> list = new ArrayList<ArticleBean>();
        for (int i = 1; i <= 20; i++) {
            ArticleBean art;
            art = new ArticleBean();
            art.setArticleId(i);
            art.setTitle("标题"+i);
            art.setImgid(R.drawable.f2);
            art.setTime("2017-4-1 15:16");
            art.setDescription("描述"+i);
            list.add(art);
        }

        return list;
    }
*/
}
